$OpenBSD: patch-sys_openbsd_c,v 1.3 2014/10/07 09:10:54 dcoppa Exp $
--- sys_openbsd.c.orig	Tue Aug 28 12:05:05 2001
+++ sys_openbsd.c	Tue Oct  7 10:35:25 2014
@@ -19,10 +19,11 @@
 
 #include <stdlib.h>
 #include <unistd.h>
-#include <sys/dkstat.h>
 #include <sys/param.h>
 #include <sys/sysctl.h>
 #include <sys/resource.h>
+#include <sys/tree.h>
+#include <sys/sched.h>
 
 #include <uvm/uvm_object.h>
 #include <uvm/uvm_extern.h>
@@ -77,6 +78,7 @@ int system_memory(void)
 {
 #define pagetob(size) ((size) << (uvmexp.pageshift))
 	struct uvmexp uvmexp;
+	struct swapent *swdev;
 	int nswap, rnswap, i;
 	int mib[] = { CTL_VM, VM_UVMEXP };
 	size_t size = sizeof (uvmexp);
@@ -84,19 +86,22 @@ int system_memory(void)
 	if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0)
 		return 0;
 
-	bm.mem_used = pagetob(uvmexp.active);
-	bm.mem_max = pagetob(uvmexp.npages);
+	bm.mem_used = pagetob((uint64_t)(uvmexp.npages - uvmexp.free));
+	bm.mem_max = pagetob((uint64_t)uvmexp.npages);
 	bm.swap_used = 0;
 	bm.swap_max = 0;
 	if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) != 0) {
-		struct swapent *swdev = malloc(nswap * sizeof(*swdev));
-		if((rnswap = swapctl(SWAP_STATS, swdev, nswap)) != nswap) {
+		swdev = calloc(nswap, sizeof(*swdev));
+		if((rnswap = swapctl(SWAP_STATS, swdev, nswap)) == nswap) {
 			for (i = 0; i < nswap; i++) {
 				if (swdev[i].se_flags & SWF_ENABLE) {
 					bm.swap_used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
 					bm.swap_max += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
 				}
 			}
+			/* Convert back to bytes */
+			bm.swap_max *= 1024;
+			bm.swap_used *= 1024;
 		}
 		free(swdev);
 	}
